<?php

/**
* Content code
* @package sqlauth
* @Copyright (C) 12/04/2006 heriniaina.eugene
* @ All rights reserved
* @ Released copyrighted
* @version 0.1
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require_once($mosConfig_absolute_path."/administrator/components/com_sqlauth/class.sqlauth.php");
require_once( $mainframe->getPath( 'admin_html' ) );

switch ($task) {
  case "save":
    save( $option );
    break;
  default:
    edit( $option, $id[0] );
    break;
}

function save( $option ) {
  global $database, $my;

	$sqlauth_cfg_id = mosGetParam( $_REQUEST, 'sqlauth_cfg_id', '' );
	$sqlauth_cfg_value = mosGetParam( $_REQUEST, 'sqlauth_cfg_value', '' );

	$configSaved = true;


	if( is_array($sqlauth_cfg_id) ) {
		$configSaved = true;
		for ($i=0; $i<count($sqlauth_cfg_id); $i++) {
			$row = new jsqlauth($database);
			$row->id = $sqlauth_cfg_id[$i];
			$row->value = $sqlauth_cfg_value[$i];
			if( !$row->store() ) {
				echo "Error: ".$row->getError()."<br />";
				$configSaved = false;
			}
		}
	}

	if($configSaved) {
		mosRedirect( "index2.php?option=$option", "Saved" );
	} else {
		mosRedirect( "index2.php?option=$option", "Not Saved" );
	}

}


function edit( $option) {
	global $database;

	$query = "SELECT * "
	. "\n FROM #__sqlauth ";
	$database->setQuery( $query );

	if(!$result = $database->query()) {
	echo $database->stderr();
	return;
	}
	$rows = $database->loadObjectList();
  HTML_sqlauth::edit( $rows, $option );
}
